{# 创建三级下拉框 #}
{% macro three_level_select(label='',level1_name='', level2_name='', level3_name='', not_null='') %}
<div class="form-group">
    <input type="hidden" id="db_level1_name" value="{{ level1_name }}">
    <input type="hidden" id="db_level2_name" value="{{ level2_name }}">
    <input type="hidden" id="db_level3_name" value="{{ level3_name }}">
    {% if not_null == 'yes' %}
    <label class="col-lg-4 control-label"><span style="color: red;">*</span> {{ label }}</label>
    {% else %}
    <label class="col-lg-4 control-label">{{ label }}</label>
    {% endif %}
    <div class="col-lg-8">
        {# 动态level1 #}
        <div class="row">
            {# 动态level1 #}
            <div class="col-lg-4" style="padding-right: 0%;">
                <select id="level1_name" name="level1_name" class="form-control">
                </select>
            </div>
            <div class="col-lg-4" style="padding-right: 2%;padding-left: 2%;">
                <select id="level2_name" name="level2_name" style="display:none;" class="form-control">
                </select>
            </div>
            <div class="col-lg-4" style="padding-left: 0%;">
                <select id="level3_name" name="level3_name" style="display:none;" class="form-control">
                </select>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
$(document).ready(function() {
    var level1_name = $('#db_level1_name').val();
    var level2_name = $('#db_level2_name').val();
    var level3_name = $('#db_level3_name').val();
    if (level1_name && level2_name && level3_name || level1_name && level2_name){
        $.get('/sys/cat_name/show', {'level1_name':level1_name, 'level2_name':level2_name, 'level3_name':level3_name}, function(res){
            $('#level1_name').append(res.data.level1_html);
            $('#level2_name').show();
            $('#level2_name').append(res.data.level2_html);
            $('#level3_name').show();
            $('#level3_name').append(res.data.level3_html);
        });
    }else{
        $('#level2_name option').remove();
        $('#level3_name option').remove();
        $('#level2_name').hide();
        $('#level3_name').hide();
        $.get('/sys/cat_name/show', {'level1_name':level1_name}, function(res){
            $('#level1_name').append(res.data.level1_html);
            $('#level1_name').change();
        });
    }
});
$('#level1_name').change(function(){
    var level1_id = $('#level1_name').val();
    var selected_level2_name = $('#db_level2_name').val();
    $('#level2_name option').remove();
    $('#level3_name option').remove();
    $('#level2_name').hide();
    $('#level3_name').hide();

    if (!level1_id < 0) {
        return;
    }
    $.get('/sys/level_name/options', {'level1_id':level1_id, 'level2_name':selected_level2_name}, function(res){
        $('#level2_name').show();
        $('#level2_name').append(res.data.options_html);
    });
});
$('#level2_name').change(function(){
    var level2_id = $('#level2_name').val();
    var selected_level3_name = $('#db_level3_name').val();
    $('#level3_name option').remove();
    $('#level3_name').hide();

    if (!level2_id < 0) {
        return;
    }
    $.get('/sys/level_name/options', {'level2_id':level2_id, 'level3_name':selected_level3_name}, function(res){
        $('#level3_name').append(res.data.options_html);
        $('#level3_name').show();
    });
});


</script>
{% endmacro %}


{% macro double_input(label1, name1, label2, name2, value1='', placeholder1='', type1='text', required1='', help1='', disabled1='',  value2='', placeholder2='', type2='text', required2='', help2='', disabled2='') %}
{{ set_double_errmsg() }}
<div class="form-group">
    <div class="row">
        <div class="col-lg-6 col-md-6 col-sm-6">
            <label class="col-lg-4 control-label" id="label-{{ name1 }}">{{ label1 }}</label>
            <div class="col-lg-8 {{error_class1}}" id="control-{{ name1 }}">
                {% if disabled1 == 'yes' %}
                    <input class="form-control " id="{{ name1 }}" name="{{ name1 }}" value="{{ value1|string }}"
                    placeholder="{{ placeholder1 }}" type="{{ type1 }}" {{ required1 }} onfocus=this.blur() readonly="true">
                {% else %}
                    <input class="form-control " id="{{ name1 }}" name="{{ name1 }}" value="{{ value1|string }}"
                    placeholder="{{ placeholder1 }}" type="{{ type1 }}" {{ required1 }}>
                {% endif %}
                {% if help1 %}<span class="help-block m-b-none">{{ help1|safe }}</span>{% endif %}
                {% if errmsg_text1 %}<span class="help-block m-b-none">{{ errmsg_text1 }}</span>{% endif %}
            </div>
        </div>
        <div class="col-lg-6 col-md-6 col-sm-6">
            <label class="col-lg-4 control-label" id="label-{{ name2 }}">{{ label2 }}</label>
            <div class="col-lg-8 {{error_class2}}" id="control-{{ name2 }}">
                {% if disabled2 == 'yes' %}
                    <input class="form-control " id="{{ name2 }}" name="{{ name2 }}" value="{{ value2|string }}"
                    placeholder="{{ placeholder2 }}" type="{{ type2 }}" {{ required2 }} onfocus=this.blur() readonly="true">
                {% else %}
                    <input class="form-control " id="{{ name2 }}" name="{{ name2 }}" value="{{ value2|string }}"
                    placeholder="{{ placeholder2 }}" type="{{ type2 }}" {{ required2 }}>
                {% endif %}
                {% if help2 %}<span class="help-block m-b-none">{{ help2|safe }}</span>{% endif %}
                {% if errmsg_text2 %}<span class="help-block m-b-none">{{ errmsg_text2 }}</span>{% endif %}
            </div>
        </div>
    </div>
</div>
{% endmacro %}


{% macro double_select(label1, name1, label2, name2, value1='', option_list1=[], size1='1', required1='', help1='', disabled1='', value2='', option_list2=[], size2='1', required2='', help2='', disabled2='') %}
{{ set_double_errmsg() }}
<div class="row">
    <div class="col-lg-6 col-md-6 col-sm-6">
        <div class="form-group">
            <label class="col-lg-4 control-label">{{ label1 }}</label>
            <div class="col-lg-8 {{error_class1}}">
                <select class="form-control" id="{{ name1 }}" name="{{ name1 }}" size="{{ size1 }}" {{ required1 }} {{ disabled1 }}>
                {% for option in option_list1 %}
                    {% if option[1]|string == value1|string %}
                        <option value="{{ option[1] }}" selected="selected">{{ option[0] }}</option>
                    {% else %}
                        <option value="{{ option[1] }}">{{ option[0] }}</option>
                    {% endif %}
                {% endfor %}
                </select>
                {% if help1 %}<span class="help-block m-b-none">{{ help1 }}</span>{% endif %}
                {% if errmsg_text1 %}<span class="help-block m-b-none">{{ errmsg_text1 }}</span>{% endif %}
            </div>
        </div>
    </div>
    <div class="col-lg-6 col-md-6 col-sm-6">
        <div class="form-group">
            <label class="col-lg-4 control-label">{{ label2 }}</label>
            <div class="col-lg-8 {{error_class2}}">
                <select class="form-control" id="{{ name2 }}" name="{{ name2 }}" size="{{ size2 }}" {{ required2 }} {{ disabled2 }}>
                {% for option in option_list2 %}
                    {% if option[1]|string == value2|string %}
                        <option value="{{ option[1] }}" selected="selected">{{ option[0] }}</option>
                    {% else %}
                        <option value="{{ option[1] }}">{{ option[0] }}</option>
                    {% endif %}
                {% endfor %}
                </select>
                {% if help2 %}<span class="help-block m-b-none">{{ help2 }}</span>{% endif %}
                {% if errmsg_text2 %}<span class="help-block m-b-none">{{ errmsg_text2 }}</span>{% endif %}
            </div>
        </div>
    </div>
</div>
{% endmacro %}

{% macro dbselect(label, name, value='', option_list=[], att_name='', attr_value='', size='1', required='', help='', disabled='') %}
{{ set_double_errmsg() }}
<div class="form-group">
    <label class="col-lg-4 control-label">{{ label }}</label>
    <div class="col-lg-8 {{error_class}}">
        {% if disabled == 'yes'%}
        <select class="form-control" id="{{ name }}" name="{{ name }}" size="{{ size }}" {{ required }} onfocus=this.blur() readonly="true">
        {% else %}
        <select class="form-control" id="{{ name }}" name="{{ name }}" size="{{ size }}" {{ required }} {{ disabled }}>
        {% endif %}
            <option value="-1">请选择……</option>
        {% for option in option_list %}
            {% set val = option|getattr(attr_value) %}
            {% set selected = 'selected="selected"' if val|string == value|string else '' %}
            <option value="{{ val }}" {{selected}}>{{ option|getattr(att_name) }}</option>
        {% endfor %}
        </select>
        {% if help %}<span class="help-block m-b-none">{{ help }}</span>{% endif %}
        {% if errmsg_text %}<span class="help-block m-b-none">{{ errmsg_text }}</span>{% endif %}
    </div>
</div>
{% endmacro %}


{% macro textarea(label, name, value='', required='', disabled='', help='', height='35px') %}
{{ set_double_errmsg() }}
<div class="form-group">
    <label class="col-lg-4 control-label">{{ label }}</label>
    <div class="col-lg-8 {{error_class}}">
        {% if disabled == 'yes'%}
        <textarea id="{{ name }}" class="form-control" name="{{ name }}" {{ required }} style="overflow:hidden;max-width: 100%;min-height: {{ height  }};" onfocus=this.blur() readonly="true">{{ value }}</textarea>
        {%  else %}
        <textarea id="{{ name }}" class="form-control" name="{{ name }}" {{ required }} style="overflow:hidden;max-width: 100%;min-height: {{ height }};">{{ value }}</textarea>
        {% endif %}
        {% if help %}<span class="help-block m-b-none">{{ help }}</span>{% endif %}
        {% if errmsg_text %}<span class="help-block m-b-none">{{ errmsg_text }}</span>{% endif %}
    </div>
</div>
<script type="text/javascript">
var s =  document.querySelectorAll("textarea");
for(var i =0; i<s.length;i++){
   s[i].style.height =  s[i].scrollHeight + 10+ 'px';
}
</script>
{% endmacro %}


{% macro set_double_errmsg() %}
{% set errmsg1 = g['errmsg1'] %}
{% set errmsg_text1 = errmsg1.get(name1, '') if errmsg1 else '' %}
{% set error_class1 = 'has-error' if errmsg_text1 else '' %}
{% set errmsg2 = g['errmsg2'] %}
{% set errmsg_text2 = errmsg2.get(name2, '') if errmsg2 else '' %}
{% set error_class2 = 'has-error' if errmsg_text2 else '' %}
{% endmacro %}
